package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.search.service.repository.AlbumIndexRepository;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    /**
     * 检索
     * @param albumIndexQuery
     * @return
     */
    @Operation(summary = "检索")
    @PostMapping
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery){
//        调用服务层方法
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }


    /**
     * 批量上架
     * @return
     */
    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAlbum")
    public Result batchUpperAlbum(){
        //  循环
        for (long i = 1; i <= 1500; i++) {
            searchService.upperAlbum(i);
        }
        //  返回数据
        return Result.ok();
    }

    /**
     * 根据专辑id上架专辑功能
     */
    @Operation(summary = "根据专辑id上架专辑")
    @GetMapping("/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId){
//        1.调用服务层方法
        searchService.upperAlbum(albumId);
        return Result.ok();
    }



    /**
     * 根据专辑id下架专辑功能
     */
    @Operation(summary = "根据专辑id下架专辑")
    @GetMapping("/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId){
//        1.调用服务层方法
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    /**
     * 根据一级分类id，获取置顶到频道页的三级分类的热门数据
     */
    @Operation(summary = "获取频道页数据")
    @GetMapping("/channel/{category1Id}")
    public Result channel(@PathVariable Long category1Id){
//        map的key对应实体类属性，map的value是实体类的属性值
       List <Map<String,Object>> mapList =  searchService.channel(category1Id);
       return Result.ok(mapList);
    }

    /**
     * 自动补全功能
     */
    @Operation(summary = "关键字自动补全")
    @GetMapping("/completeSuggest/{keyword}")
    public Result completeSuggest(@PathVariable String keyword){
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }


    /**
     * 更新排行榜
     */
    @SneakyThrows
    @Operation(summary = "更新排行榜")
    @GetMapping("/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking(){
//        1.调用服务层方法
        searchService.updateLatelyAlbumRanking();
//        2.默认返回
        return Result.ok();
    }


    /**
     * 获取排行榜列表
     */
    @Operation(summary = "获取排行榜列表")
    @Parameters({
            @Parameter(name = "category1Id",description = "一级分类",in = ParameterIn.PATH,required = true),
            @Parameter(name = "dimension",description = "热度:hotScore、播放量:playStatNum、订阅量:subscribeStatNum、购买量:buyStatNum、评论数:albumCommentStatNum",required = true,in = ParameterIn.PATH),
    })
    @GetMapping("/findRankingList/{category1Id}/{ranking}")
    public Result<List<AlbumInfoIndex>> findRankingList(@PathVariable Long category1Id,
                                                          @PathVariable String ranking){
//        1.调用服务层方法
        List<AlbumInfoIndex> infoIndexList = searchService.findRankingList(category1Id,ranking);
//        2.返回结果集
        return Result.ok(infoIndexList);
    }

//    /**
//     * 根据专辑id获取专辑信息
//     */
//    @Operation(summary = "根据专辑id获取专辑信息")
//    @GetMapping("/{albumId}")
//    public Result getAlbumInfoById(@PathVariable Long albumId){
//        Result<AlbumInfo> albumInfo = albumInfoFeignClient.getAlbumInfo(albumId);
//        return Result.ok(albumInfo);
//
//    }
}

